﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;

/// <summary>
/// Summary description for GetLoai
/// </summary>
public class LoaiDA
{
    Connection conn = new Connection();
    
    public List<Loai> GetData(int thuonghieuid)
    {
        try
        {
            List<Loai> lstLoai = new List<Loai>();
            conn.openCon();
            string sql = "SELECT * FROM [Loai] as l where ID in(select LoaiID from sanpham where sanpham.loaiid=l.id and sanpham.nhanhieuid=@id group by LoaiID) and Xoa=0 order by SapXep ASC";
            SqlCommand cmd = new SqlCommand(sql, conn.getCon());
            cmd.Parameters.Add("@id", System.Data.SqlDbType.Int).Value = thuonghieuid;
            SqlDataReader reader = cmd.ExecuteReader();
            Loai objLoai = null;
            while (reader.Read())
            {
                objLoai = new Loai(reader.GetInt32(0), reader.GetString(1), reader.GetBoolean(3));
                lstLoai.Add(objLoai);
            }
            reader.Close();
            conn.closeCon();
            return lstLoai;
        }
        catch (Exception)
        {

            throw;
        }
        finally {
            conn.closeCon();
        }
    }
    public List<Loai> GetData()
    {
        try
        {
            List<Loai> lstLoai = new List<Loai>();
            conn.openCon();
            string sql = "SELECT * FROM [Loai] as l where Xoa=0 order by SapXep ASC";
            SqlCommand cmd = new SqlCommand(sql, conn.getCon());
            SqlDataReader reader = cmd.ExecuteReader();
            Loai objLoai = null;
            while (reader.Read())
            {
                objLoai = new Loai(reader.GetInt32(0), reader.GetString(1), reader.GetBoolean(3));
                lstLoai.Add(objLoai);
            }
            reader.Close();
            conn.closeCon();
            return lstLoai;
        }
        catch (Exception)
        {

            throw;
        }
        finally
        {
            conn.closeCon();
        }
    }
}